<?php defined('BASEPATH') || exit('No direct script access allowed');

class Buildingsyuyue_model extends BF_Model
{
    protected $table_name	= 'building_yuyue';
	protected $key			= 'id';
	protected $date_format	= 'datetime';

	protected $log_user 	= false;
    protected $set_created	= true;
	protected $set_modified = false;
	protected $soft_deletes	= false;

    protected $created_field     = 'created_on';

	// Customize the operations of the model without recreating the insert,
    // update, etc. methods by adding the method names to act as callbacks here.
	protected $before_insert 	= array();
	protected $after_insert 	= array();
	protected $before_update 	= array();
	protected $after_update 	= array();
	protected $before_find 	    = array();
	protected $after_find 		= array('set_phone_info');
	protected $before_delete 	= array();
	protected $after_delete 	= array();

	// For performance reasons, you may require your model to NOT return the id
	// of the last inserted row as it is a bit of a slow method. This is
    // primarily helpful when running big loops over data.
	protected $return_insert_id = true;

	// The default type for returned row data.
	protected $return_type = 'object';

	// Items that are always removed from data prior to inserts or updates.
	protected $protected_attributes = array();

	// You may need to move certain rules (like required) into the
	// $insert_validation_rules array and out of the standard validation array.
	// That way it is only required during inserts, not updates which may only
	// be updating a portion of the data.
	protected $validation_rules 		= array(

	);
	protected $insert_validation_rules  = array();
	protected $skip_validation 			= false;

    /**
     * Constructor
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function prep_data($post_data) {
        $data = parent::prep_data($post_data);

        return $data;
    }

    protected function set_phone_info($row) {
        $this->load->library('phone/phone');
        $row->phone_info = $this->phone->find($row->telphone);
        return $row;
    }

    //根据日期获取相应数据
    public function getDataByDate($buildingId, $code, $key = 0){
        // return $buildingId;
        if (empty($buildingId)) return false;
        $where = " building_id = ".$buildingId." and type != 1 and building_code = '$code'";
        if($key == 1){        //今日线索
            $where .= " and created_on >= '".date("Y-m-d 00:00:00")."' AND created_on <= '".date("Y-m-d 23:59:59")."'";
            $info = $this->find_all_by($where);
        }else if($key == 2){  //昨日线索
            $where .= " and created_on >= '".date("Y-m-d 00:00:00",strtotime("-1 day"))."' AND created_on <= '".date("Y-m-d 23:59:59",strtotime("-1 day"))."'";
            $info = $this->find_all_by($where);
        }else{ //所有线索
            $info = $this->find_all_by($where);
        }
        if(!empty($info)){
            $this->load->model('black_model');
            foreach ($info as $key => $value) {
                $res = $this->black_model->find_by( ['phone' => $value->telphone] );
                if( !$res ){ $data[] = $value; }
            }
        }
        return isset($data) ? $data : null;
    }
}